package org.example.onetry.dao;



import org.example.onetry.dbu4.DbUtil;
import org.example.onetry.user.users;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class paqdao {
//单行查询
    public users queryone(Integer id){
        Connection cn = DbUtil.getConn();
        String sql = "select * from agry where id=?";
        try {
            PreparedStatement ps = cn.prepareStatement(sql);
            ps.setInt(1, id);
            ResultSet cs = ps.executeQuery();
            if(cs.next()){
                users s = new users(
                        cs.getInt("id"),
                        cs.getString("na"),
                        cs.getString("con"));
                return s;
            }
        }catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtil.closeConn();
        }
        return null;
    }

//查询所有
    public List<users>queryall() {
        List<users> ls = new ArrayList<users>();
        Connection cn = DbUtil.getConn();
        try {
            String select = "select * from agry";
            PreparedStatement preparedStatement = cn.prepareStatement(select);
            ResultSet rs = preparedStatement.executeQuery();
            while (rs.next()) {
                //将查询结果的每行数据 封装为对象 并加入到集合内
                users pp = new users(
                        rs.getInt("id"),
                        rs.getString("na"),
                        rs.getString("con")
                );
                ls.add(pp);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtil.closeConn();
        }

        return ls;
    }
}
